<?php

namespace app\index\library\wechat;

use fast\Http;
use think\Cache;
use think\Db;

class MiniApp
{
    public static function getAccessToken($appid,$appsecret)
    {
        //tocken缓存
        $key = 'wanlshop_acctocken';
        if (Cache::store('redis')->has($key)){
            $access_token = Cache::store('redis')->get($key);
        }else{
            $params = [
                'grant_type'  => 'client_credential',
                'appid'       => $appid,
                'secret'      => $appsecret
            ];

            $result = Http::sendRequest("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential", $params, 'GET');

            if (!$result['ret']) {
                return "error 001";
            }

            $jsoninfo = (array)json_decode($result['msg'], true);
            $access_token = $jsoninfo['access_token'];
            Cache::store('redis')->set($key,$access_token,7000);
        }

        return $access_token;

    }

    public static function getOtherAccessToken($appid,$appsecret,$key = 'wanlshop_other_acctocken')
    {
        //tocken缓存
        if (Cache::store('redis')->has($key)){
            $access_token = Cache::store('redis')->get($key);
        }else{
            $params = [
                'grant_type'  => 'client_credential',
                'appid'       => $appid,
                'secret'      => $appsecret
            ];

            $result = Http::sendRequest("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential", $params, 'GET');

            if (!$result['ret']) {
                return "error 001";
            }

            $jsoninfo = (array)json_decode($result['msg'], true);
            $access_token = $jsoninfo['access_token']??'';
            Cache::store('redis')->set($key,$access_token,7000);
        }

        return $access_token;
    }

    public static function create_wx_qrcode_tmp($token,$page,$scene)
    {
        //最小 280px，最大 1280px
        $params = [
            'scene'      => $scene,
            'page'       => $page,
            'width'      => 280
        ];
        $json_p = json_encode($params);
        $result = Http::sendRequest("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=".$token, $json_p);

        if (!$result['ret']) {
            //dump($result['ret']);
            return false;
        }
        if($result['ret'] && $result['msg']){
            $msg = (array)json_decode($result['msg']);
            if(isset($msg['errcode']) && $msg['errcode'] == 40001){
                //token过期
                return "40001";
            }
        }

        $base64_image = "data:image/jpeg;base64,".base64_encode($result['msg']);
        return $base64_image;
    }

    public static function getAppInfo($appid,$type='miniapp'){
        // 查询数据库appid 对应的密钥
        $result = Db::name('wechat_config')->where(['appid'=>$appid,'type'=>$type])->find();
        if (!$result){
            return false;
        }
        return $result;
    }
}